package com.xc.controller;


import com.xc.enums.BizCodeEnum;
import com.xc.request.UserLoginRequest;
import com.xc.request.UserRegisterRequest;
import com.xc.service.FileService;
import com.xc.service.UserService;
import com.xc.utils.JsonData;
import com.xc.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xingchen
 * @since 2021-10-17
 */

@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {


    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    /**
     * 上传用户头像
     * 默认最大是1M，超过会报错
     *
     * @param file
     * @return
     */
    @ApiOperation("用户头像上传")
    @PostMapping("upload")
    public JsonData uploadUserImg(
            @ApiParam(value = "文件上传", required = true)
            @RequestPart("file") MultipartFile file) {
        String result = fileService.uploadUser(file);

        return result != null ? JsonData.buildSuccess(result) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);

    }

    /**
     * 用户注册
     *
     * @param userRegisterRequest
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("register")
    public JsonData register(@ApiParam("用户注册对象") @RequestBody UserRegisterRequest userRegisterRequest) {

        JsonData jsonData = userService.register(userRegisterRequest);
        return jsonData;
    }



    /**
     * 用户登录
     * @param userLoginRequest
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("login")
    public JsonData login(@ApiParam("用户登录对象")@RequestBody UserLoginRequest userLoginRequest){

        JsonData jsonData = userService.login(userLoginRequest);
        return jsonData;
    }

    /**
     * 用户个人信息查询
     * @return
     */
    @ApiOperation("个人信息查询")
    @GetMapping("detail")
    public JsonData detail(){
        UserVO userVO =userService.findUserDetail();
        return JsonData.buildSuccess(userVO);
    }

}


